package com.chengdu.chun.controller;

import com.chengdu.chun.po.Classes;
import com.chengdu.chun.po.Room;
import com.chengdu.chun.po.Student;
import com.chengdu.chun.po.User;
import com.chengdu.chun.service.ClassesService;
import com.chengdu.chun.service.RoomService;
import com.chengdu.chun.service.StudentService;
import com.chengdu.chun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private HttpSession session;

    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassesService classesService;
    @Autowired
    private UserService userService;
    @Autowired
    private RoomService roomService;


//    @GetMapping("/studentsbyclass")
//    public ModelAndView studentsbyclass(int cid) {
//        if (judguUser()) return new ModelAndView("redirect:/login.html");
//        ModelAndView mv = new ModelAndView("studentsbyclass");
//
//        if (cid <= 0) {
//            mv.addObject("errinfo", "没有正确的班级序号！！！");
//            mv.setViewName("err");
//        }
//
//        List<Student> studentList = studentService.getStudentByClasses(cid);
//        Classes classes = classesService.getClassesBycid(cid);
//
//        mv.addObject("studentList", studentList);
//        session.setAttribute("classes", classes);
//
//        return mv;
//    }

    /**
     * ModelAndView 使用时作为方法的返回参数，同时在方法中必须显示的使用new关键字进行创建
     * Model        使用时写入到方法的参数里面（自动注入），另外方法的返回值则定义为String
     *
     * @param model
     * @param cid
     * @return
     */
    @GetMapping("/studentsbyclass/{cid}.html")
    public String studentsbyclass(Model model, @PathVariable int cid) {
        List<Student> studentList = studentService.getStudentByClasses(cid);
        Classes classes = classesService.getClassesBycid(cid);

        model.addAttribute("studentList", studentList);
        session.setAttribute("classes", classes);

        return "studentsbyclass";
    }

    @GetMapping("/studentsbyroom/{rid}.html")
    public String studentsbyroom(Model model, @PathVariable int rid) {
        List<Student> studentList = studentService.getStudentByRoom(rid);
        Room room = roomService.getRoomByrid(rid);

        model.addAttribute("studentList", studentList);
        session.setAttribute("room", room);

        return "studentsbyclass";
    }



    /**
     * 根据名称或者手机号查找
     *
     * @param sname
     * @param mobile
     * @return
     */
    @GetMapping("/studentsbysnameandmobile")
    public ModelAndView studentsbysnameandmobile(String sname, String mobile) {
//        if (judguUser()) return new ModelAndView("redirect:/login.html");
        ModelAndView mv = new ModelAndView();
        List<Student> studentList = studentService.getStudentBySnameAndMobile(((Classes) session.getAttribute("classes")).getCid(), sname, mobile);
        mv.addObject("studentList", studentList);
        mv.setViewName("studentsbyclass");
        return mv;
    }


    @GetMapping("/addstudentpage/{cid}")
    public String addclassespage(Model model, @PathVariable Integer cid) {
//        if (judguUser()) return "redirect:/login.html";
        Classes classes = classesService.getClassesBycid(cid);
        model.addAttribute("classes", classes);
        return "addstudent";
    }

    /**
     * 添加新的学生，相关参数值该如何书写
     *
     * @return
     */
    @PostMapping("/addstudent")
    /**
     * 可以使用对象的形式进行请求参数的接收，但是注意：参数名称必须和类的属性同名，且数据类型必须保持一致（或者能被自动转换）
     */
    public String addclasses(Model model, Student student) {
//        if (judguUser()) return "redirect:/login.html";
        // 调用添加学生的参数
        int num = studentService.insertStudent(student);
        System.out.println(num);
        if (num <= 0) {
            model.addAttribute("errinfo", "添加新学生失败！！！");
            return "err";
        }

        return "redirect:/student/studentsbyclass/" + student.getCid() + ".html";
    }

    @GetMapping("/studentdetail/{sid}")
    public String studentdetail(Model model, @PathVariable Integer sid) {
//        if (judguUser()) return "redirect:/login.html";
        System.out.println("学生编号: " + sid);
        Student student = studentService.studentInfoBysid(sid);
        if (student == null) {
            model.addAttribute("errinfo", "查看详细信息失败！！！");
            return "err";
        }
        Classes classes = classesService.getClassesBycid(student.getCid());
        User user = userService.userinfobyuid(Integer.valueOf(classes.getUid()+""));

        model.addAttribute("student",student);
        model.addAttribute("classes",classes);
        model.addAttribute("teacher",user);


        return "studentdetail";
    }

    @GetMapping("/ajaxchangestatus")
    @ResponseBody // 确保返回的是Ajax的格式数据，因为只有字母内容，所以可以省略字符集处理produces = "application/json;charset=utf-8"
    public String ajaxchangestatus(Model model,Integer sid, Integer cstatus){
//        if (judguUser()) return "redirect:/login.html";
        return studentService.changeStatus(cstatus, sid)?"ok":"no";
    }


//    private boolean judguUser() {
//        if (session.getAttribute("user") == null) {
//            return true;
//        }
//        return false;
//    }
}
